<?php

namespace app\common\controller;
use \think\Controller;
class Wxlogin extends Controller
{   
    private $appid = 'wx61d62fce6d4ba436';
    private $appsecret = '02a3302b09627facbefd077f17aae50b';

    //獲取code
    public function getCode($url)
    {   
        $REDIRECT_URI = $url;
        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->appid.'&redirect_uri='.$REDIRECT_URI.'&response_type=code&scope=snsapi_base&state=snsapi_base#wechat_redirect';
        $this->redirect($url,302);
    }

    public function getOpenid($code)
    {
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->appid}&secret={$this->appsecret}&code={$code}&grant_type=authorization_code";
        return file_get_contents($url);
    }
}